package com.example.mysaat.service_menu_manual_test.allActivities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;

import com.example.mysaat.R;
import com.example.mysaat.service_menu_manual_test.Listeners.MyFailOnclickListener;
import com.example.mysaat.service_menu_manual_test.Listeners.MyPassOnclickListener;
import com.example.mysaat.service_menu_manual_test.Listeners.MySeekBarChangeListener;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//问题：当耳机拔出来的时候停止播放音乐
public class RingActivity extends AppCompatActivity {
    private Button pass_btn = null;
    private Button fail_btn = null;
    private Switch mSwitch = null;
    private SeekBar seekBar = null;
    private boolean isPlaying = false;
    private AudioManager maudioManager=null;
    private MediaPlayer mediaPlayer = null;
    private static final String TAG = "RingActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ring);
        init();
        initContent();
    }

    private void init(){
        pass_btn = findViewById(R.id.brightness_pass_btn);
        fail_btn= findViewById(R.id.brightness_fail_btn);
        seekBar = findViewById(R.id.ring_exit_stev);
        mSwitch = findViewById(R.id.switch_on_off);
        maudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        maudioManager.setMicrophoneMute(false); //始终是扬声器播放
        maudioManager.setSpeakerphoneOn(true);
        maudioManager.setMode(AudioManager.STREAM_MUSIC);
    }

    private void initContent(){
        seekBar.setOnSeekBarChangeListener(new MySeekBarChangeListener(this));
        pass_btn.setOnClickListener(new MyPassOnclickListener(RingActivity.this,null));
        fail_btn.setOnClickListener(new MyFailOnclickListener(RingActivity.this,null));
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    play();
                }else{
                    stop();
                }
            }
        });
    }

    private void play(){
        if(mediaPlayer==null)
        mediaPlayer = MediaPlayer.create(RingActivity.this,R.raw.test_ringtone);
        isPlaying = true;
        try {
        if(!mediaPlayer.isPlaying()){
            mediaPlayer.setLooping(true);   //循环播放
            mediaPlayer.start();
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stop(){
     try{
        if(isPlaying){
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
            isPlaying=false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mediaPlayer!=null){
            mediaPlayer.release();
            mediaPlayer=null;
        }
    }
}
